package com.wtg;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;

/**
 * Shiro 入门练习
 *
 */
public class App1
{
    public static void main( String[] args )
    {
        //1: 创建权限管理器
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        securityManager.setRealm(new IniRealm("classpath:users.ini"));
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("tom","123");
        try {
            System.out.println("验证前："+subject.isAuthenticated());
            subject.login(token);
            System.out.println("验证后："+subject.isAuthenticated());
            System.out.println("登录成功");
        }catch (IncorrectCredentialsException e)
        {
            System.out.println("密码与账号不一致");
        }catch (UnknownAccountException e){
            System.out.println("用户名不存在");
        }



    }
}
